//
//  PlayerTwoController.m
//  TFY_PlayerView
//
//  Created by 田风有 on 2019/7/3.
//  Copyright © 2019 田风有. All rights reserved.
//

#import "PlayerTwoController.h"
#import "TFY_PlayerToolsHeader.h"
#import "TFY_PlayerControllerView.h"
#import "PlayerVideoModel.h"

static NSString *kVideoCover = @"https://upload-images.jianshu.io/upload_images/635942-14593722fe3f0695.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240";
@interface PlayerTwoController ()
@property (nonatomic, strong) TFY_PlayerController *player;
@property (nonatomic, strong) UIImageView *imageViews;
@property (nonatomic, strong) TFY_PlayerControllerView *controlView;
@property (nonatomic, strong) UIButton *playBtn;
@property(nonatomic , strong)PlayerVideoModel *video;
@end

@implementation PlayerTwoController

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.player.viewControllerDisappear = YES;
   
    NSDictionary *dict = [self.player ModelDict];
    
    NSLog(@"------------------------------%@",dict);
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    self.player.viewControllerDisappear = NO;
}


-(void)setModels:(Data *)models{
    _models = models;
    
        [self show_url];
}


-(void)show_url{
    VideoCommand *command = [VideoCommand new];
    command.ids = self.models.id;
    [[command.showCommand execute:@1] subscribeNext:^(id  _Nullable x) {
        
        if ([x isKindOfClass:[PlayerVideoModel class]]) {
            self.video = x;
            NSMutableArray *array = [NSMutableArray array];
            
            [self.video.data.zu enumerateObjectsUsingBlock:^(Zu * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                
                [obj.ji enumerateObjectsUsingBlock:^(Ji * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    
                    TFY_PlayerVideoModel *modes = [TFY_PlayerVideoModel new];
                    modes.tfy_name = [NSString stringWithFormat:@"%@%@",self.video.data.name,obj.name];
                    modes.tfy_url = obj.purl;
                    modes.tfy_videoId = [NSString stringWithFormat:@"%ld",obj.id];
                    modes.tfy_ids = self.models.id;
                    modes.tfy_pic = self.video.data.pic;
                    
                    [array addObject:modes];
                    
                }];
            }];
            self.player.assetUrlMododels = array;
            self.controlView.videoCount = array.count;
            [self.player playTheIndex:0];
        }
    }];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.imageViews];
    self.imageViews.tfy_LeftSpace(0).tfy_TopSpace(0).tfy_RightSpace(0).tfy_Height(TFY_PLAYER_ScreenW*9/16);
    
    [self.imageViews addSubview:self.playBtn];
    self.playBtn.tfy_Center(0, 0).tfy_size(50, 50);
    
    
    self.player = [TFY_PlayerController playerWithPlayerManagercontainerView:self.imageViews];
    self.player.controlView = self.controlView;
    /// 设置退到后台继续播放
    self.player.pauseWhenAppResignActive = NO;
    //开启多视频连续播放
    self.player.continuouslybool = YES;
    
    TFY_PLAYER_WS(myself);
    self.player.orientationWillChange = ^(TFY_PlayerController * _Nonnull player, BOOL isFullScreen) {
        [myself setNeedsStatusBarAppearanceUpdate];
    };
    
    self.player.playvideocontinuously = ^(TFY_PlayerController *player, TFY_PlayerVideoModel * _Nonnull model) {
        
        myself.controlView.currentPlayIndex = player.currentPlayIndex;
        
        [myself.controlView showTitle:model.tfy_name coverURLString:kVideoCover fullScreenMode:FullScreenModeLandscape];
    };
    //小窗户选集
    self.controlView.didSelectItemblock = ^(NSInteger index) {
      
        [myself.player playTheIndex:index];
    };
}

- (void)playClick:(UIButton *)sender {
//    TFY_PlayerVideoModel *modesl = [TFY_PlayerVideoModel new];
//    modesl.tfy_url = @"http://vip.okokbo.com/20180117/BNp2mT7Q/index.m3u8";
//    self.player.assetUrlModel = modesl;
//    [self.controlView showTitle:@"no.9" coverURLString:kVideoCover fullScreenMode:FullScreenModeAutomatic];
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    if (self.player.isFullScreen) {
        return UIStatusBarStyleLightContent;
    }
    return UIStatusBarStyleDefault;
}

- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation {
    return UIStatusBarAnimationSlide;
}

- (BOOL)shouldAutorotate {
    return self.player.shouldAutorotate;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    if (self.player.isFullScreen) {
        return UIInterfaceOrientationMaskLandscape;
    }
    return UIInterfaceOrientationMaskPortrait;
}


- (TFY_PlayerControllerView *)controlView {
    if (!_controlView) {
        _controlView = [[TFY_PlayerControllerView alloc] init];
        _controlView.fastViewAnimated = YES;
        _controlView.autoHiddenTimeInterval = 5;
        _controlView.autoFadeTimeInterval = 0.5;
        _controlView.prepareShowLoading = YES;
        _controlView.prepareShowControlView = YES;
    }
    return _controlView;
}

-(UIImageView *)imageViews{
    if (!_imageViews) {
        _imageViews = tfy_imageView();
        [_imageViews tfy_setImageWithURLString:kVideoCover placeholder:[UIImage imageNamed:@""]];
    }
    return _imageViews;
}
- (UIButton *)playBtn {
    if (!_playBtn) {
        _playBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_playBtn setImage:[UIImage imageNamed:@"videoImages.bundle/start_fullscreen"] forState:UIControlStateNormal];
        [_playBtn addTarget:self action:@selector(playClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _playBtn;
}
@end
